//
//  MSReleaseTradeViewController.m
//  SocietySale
//
//  Created by yumingwei on 2016/11/16.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSReleaseTradeViewController.h"
#import "MSReleaseTradeTableVC.h"
#import "MSReleaseBuyTableVC.h"
#import "MSSelectProjectViewController.h"

@interface MSReleaseTradeViewController ()<UIGestureRecognizerDelegate>
{
    NSString *saleProjectIdStr;
    NSString *buyProjectIdStr;
    NSString *saleTypeStr;
    NSString *saleStateStr;
    NSString *buyTypeStr;
    NSString *saleContractPrice;
    NSString *saleProjectPrice;
    NSString *buyContractPrice;
    NSString *buyProjectPrice;
    NSString *wallet;
}
//选择条
@property (nonatomic, strong) HDMenuView                       *menu;
@property (nonatomic, strong) GXButton                         *doneBtn;
@property (nonatomic, strong) UILabel                          *pricelabel;
@property (nonatomic, strong) UILabel                          *buyPricelabel;
@property (nonatomic, strong) NSString                         *areaPhonePrefix;
@property (nonatomic, strong) NSString                         *areaPhoneSuffix;
@property (nonatomic, assign) NSInteger                        selectedRow;
@property (nonatomic, assign) BOOL                             isAreaPhoneNum;
@property (nonatomic, strong) MSReleaseTradeTableVC            *tableVC;
@property (nonatomic, strong) MSFilterMultipleViewController   *selectVC;
@property (nonatomic, strong) MSFilterMultipleViewController   *stateSelectVC;

@property (nonatomic, strong) MSReleaseBuyTableVC              *buyTableVC;
@property (nonatomic, strong) MSFilterMultipleViewController   *buySelectVC;
@property (nonatomic, strong) MSFilterMultipleViewController   *buyStateSelectVC;
@property (nonatomic, strong) UIScrollView                     *scrollView;

@property (nonatomic, strong) NSMutableArray                   *sectionArray1;
@property (nonatomic, strong) NSMutableArray                   *buySectionArray2;

@end

@implementation MSReleaseTradeViewController

- (void)dealloc
{
    for(UIView* view in [self.contentView subviews])
    {
        [view removeFromSuperview];
    }
    
    self.contentView=nil;
    self.VctrID=nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:@"" forKey:@"phoneState"];
    [ud synchronize];
}

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"发布交易";
    leftBarBtn.hidden = NO;
}

- (void)initUI{
    self.menu = [[HDMenuView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45) titleArray:@[@"我要出售",@"我要求购"]];
    self.menu.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    self.menu.normalColor = [UIColor blackColor];
    self.menu.bottomLineColor = kBlueColor;
    self.menu.delegate = (id)self;
    self.menu.isHiddenGrayLine = NO;
    [self.contentView addSubview:self.menu];
    
    if (self.isBuy) {
        self.menu.selectedIndex = 1;
    }
    else{
        self.menu.selectedIndex = 0;
    }
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-90)];
    _scrollView.contentSize = CGSizeMake(2*SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-90);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = (id)self;
    _scrollView.scrollsToTop = NO;
    [self.contentView addSubview:_scrollView];

    self.tableVC = [[MSReleaseTradeTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-90);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:self.tableVC.view];
    _tableVC.isProjectProcess = NO;
    
    self.buyTableVC = [[MSReleaseBuyTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.buyTableVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-90);
    self.buyTableVC.enableRefresh = NO;
    self.buyTableVC.enableRequest = NO;
    self.buyTableVC.delegate = (id)self;
    self.buyTableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:self.buyTableVC.view];
    _buyTableVC.isProjectProcess = NO;
    
    [self layoutTableFooterView];
    
    self.doneBtn = [[GXButton alloc] initWithFrame:CGRectMake(0, _scrollView.bottom, SCREEN_WIDTH, 45)];
    self.doneBtn.titleLabel.font = HeitiFont(16);
    [self.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneBtn.backgroundColor = kBlueColor;
    [self.doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.doneBtn];
    
    [self creatSelectFilter];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    tapGesture.delegate = self;
    [self.contentView addGestureRecognizer:tapGesture];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShownLogin:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHiddenLogin:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedProject:) name:KSelectedProject object:nil];
    
    _isAreaPhoneNum = NO;
    _areaPhonePrefix = @"";
    _areaPhoneSuffix = @"";

    saleTypeStr = @"";
    saleStateStr = @"";
    buyTypeStr = @"";
    saleContractPrice = @"";
    saleProjectPrice = @"";
    buyContractPrice = @"";
    buyProjectPrice = @"";
    wallet = @"";
}

- (void)layoutTableFooterView{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 40)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    
    _pricelabel = [self creatLabel:CGRectMake(12, 0, SCREENWIDTH, 20)];;
    _pricelabel.text = @"该类消息平台平均交易价格 7 江湖币";
    [bgView addSubview:_pricelabel];
   
    self.tableVC.tableView.tableFooterView = bgView;
    
    //第二个表
    UIView *bgView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 40)];
    bgView2.backgroundColor = kDefaultViewBackgroundColor;
    
    _buyPricelabel = [self creatLabel:CGRectMake(12, 0, SCREENWIDTH, 40)];
    _buyPricelabel.text = @"该类消息平台平均交易价格 7 江湖币\n当前余额 43";
    _buyPricelabel.numberOfLines = 0;
    [bgView2 addSubview:_buyPricelabel];
    
    self.buyTableVC.tableView.tableFooterView = bgView2;
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    return label;
}

- (void)creatSelectFilter{
    self.selectVC = [[MSFilterMultipleViewController alloc] init];
    self.selectVC.delegate = (id<MSFilterMultipleDelegate>)self;
    self.selectVC.showConfirmBtn = NO;
    self.selectVC.isMultipleSelect = NO;
//    self.selectVC.dataArray = [@[@"业主联系人信息",@"设计联系人信息",@"施工联系人信息",@"项目进展及内部消息"] mutableCopy];
//    self.selectVC.stateArray = [@[@"NO",@"NO",@"NO",@"NO"] mutableCopy];

    self.stateSelectVC = [[MSFilterMultipleViewController alloc] init];
    self.stateSelectVC.delegate = (id<MSFilterMultipleDelegate>)self;
    self.stateSelectVC.showConfirmBtn = NO;
    self.stateSelectVC.isMultipleSelect = NO;
//    self.stateSelectVC.dataArray = [@[@"工程筹备", @"勘察设计", @"主体施工", @"设备安装", @"装饰装修", @"园林绿化", @"竣工测试"] mutableCopy];
//    self.stateSelectVC.stateArray = [@[@"NO",@"NO",@"NO",@"NO",@"NO",@"NO",@"NO"] mutableCopy];
    
    self.buySelectVC = [[MSFilterMultipleViewController alloc] init];
    self.buySelectVC.delegate = (id<MSFilterMultipleDelegate>)self;
    self.buySelectVC.showConfirmBtn = NO;
    self.buySelectVC.isMultipleSelect = NO;
//    self.buySelectVC.dataArray = [@[@"业主联系人信息",@"设计联系人信息",@"施工联系人信息",@"项目进展及内部消息"] mutableCopy];
//    self.buySelectVC.stateArray = [@[@"NO",@"NO",@"NO",@"NO"] mutableCopy];
    
    self.buyStateSelectVC = [[MSFilterMultipleViewController alloc] init];
    self.buyStateSelectVC.delegate = (id<MSFilterMultipleDelegate>)self;
    self.buyStateSelectVC.showConfirmBtn = NO;
    self.buyStateSelectVC.isMultipleSelect = NO;
//    self.buyStateSelectVC.dataArray = [@[@"工程筹备", @"勘察设计", @"主体施工", @"设备安装", @"装饰装修", @"园林绿化", @"竣工测试"] mutableCopy];
//    self.buyStateSelectVC.stateArray = [@[@"NO",@"NO",@"NO",@"NO",@"NO",@"NO",@"NO"] mutableCopy];
    
    [self fillFilterData];
}

- (void)fillFilterData{
    //项目阶段
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *codeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dict in netEngine.projectStateArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [titleArray addObject:[HDUtil check:dict[@"name"]]];
            [codeArray addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.stateSelectVC.dataArray = titleArray;
    self.stateSelectVC.stateArray = [self creatStateArrayWithArray:titleArray];
    
    //交易类别
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString *contactsCorrectFile = [documentsPath stringByAppendingPathComponent:@"tradeKindFile.plist"];
    NSMutableArray *titleArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *codeArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    if ([fileManager fileExistsAtPath:contactsCorrectFile]) {
        netEngine.tradeKindArray = [[NSMutableArray alloc] initWithContentsOfFile:contactsCorrectFile];
        for (NSDictionary *dict in netEngine.tradeKindArray) {
            if ([dict isKindOfClass:[NSDictionary class]]) {
                [titleArray2 addObject:[HDUtil check:dict[@"name"]]];
                [codeArray2 addObject:@"code"];
            }
        }
    }
    if (netEngine.tradeKindArray.count<1) {
        [netEngine requestTradeKindWithTarget:self.VctrID hudView:self.contentView];
    }
    
    self.selectVC.dataArray = titleArray2;
    self.selectVC.stateArray = [self creatStateArrayWithArray:titleArray2];
    
    self.buySelectVC.dataArray = titleArray2;
    self.buySelectVC.stateArray = [self creatStateArrayWithArray:titleArray2];
}

- (NSMutableArray *)creatStateArrayWithArray:(NSArray*)array{
    NSMutableArray *mutalArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i= 0; i<array.count; i++) {
        [mutalArray addObject:@"NO"];
    }
    return mutalArray;
}

- (void)testDataFunc{
    NSMutableArray *sectionArray;
    NSMutableArray *buySectionArray;
    if (self.model) {
        saleProjectIdStr = self.model.itemID;
        buyProjectIdStr = self.model.itemID;
        //第一个表
        sectionArray = [NSMutableArray arrayWithArray:
                        @[[DOKeyValue key:@"项目" value:self.model.title],
                          [DOKeyValue key:@"交易类别" value:@"未选择"],
                          [DOKeyValue key:@"项目阶段" value:@"未选择"]
                          ]
                        ];
        
        //第二个表
        buySectionArray = [NSMutableArray arrayWithArray:
                           @[[DOKeyValue key:@"项目" value:self.model.title],
                             [DOKeyValue key:@"交易类别" value:@"未选择"],
                             ]
                           ];
    }
    else{
        saleProjectIdStr = @"";
        buyProjectIdStr = @"";
        //第一个表
        sectionArray = [NSMutableArray arrayWithArray:
                        @[[DOKeyValue key:@"项目" value:@"未选择"],
                          [DOKeyValue key:@"交易类别" value:@"未选择"],
                          [DOKeyValue key:@"项目阶段" value:@"未选择"]
                          ]
                        ];
        
        //第二个表
        buySectionArray = [NSMutableArray arrayWithArray:
                           @[[DOKeyValue key:@"项目" value:@"未选择"],
                             [DOKeyValue key:@"交易类别" value:@"未选择"],
                             ]
                           ];
    }
    
    [netEngine requestTradeKindPriceWithTarget:self.VctrID hudView:self.contentView];
    [netEngine requestWalletWithTarget:self.VctrID hudView:self.contentView];
    
    //第一个表
    _sectionArray1 = [NSMutableArray arrayWithArray
     :@[
        [DOKeyValue key:@"姓名" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"公司" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"职位" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"电话" value:@"" placeHolder:@"请输入手机号"],
        [DOKeyValue key:@"地址" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"备注" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"价格" value:@"" placeHolder:@"未填写"],
        ]];
    self.tableVC.tradeArray = [NSMutableArray arrayWithObjects:sectionArray,_sectionArray1, nil];
    
    //第二个表
    _buySectionArray2 = [NSMutableArray arrayWithArray
     :@[
        [DOKeyValue key:@"姓名" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"职位" value:@"" placeHolder:@"未填写"],
        [DOKeyValue key:@"价格" value:@"" placeHolder:@"未填写"],
        ]];
    self.buyTableVC.tradeArray = [NSMutableArray arrayWithObjects:buySectionArray,_buySectionArray2, nil];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"TradeKind"]) {
            NSMutableArray *titleArray2 = [[NSMutableArray alloc] initWithCapacity:0];
            NSMutableArray *codeArray2 = [[NSMutableArray alloc] initWithCapacity:0];
            for (NSDictionary *dict in netEngine.tradeKindArray) {
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    [titleArray2 addObject:[HDUtil check:dict[@"name"]]];
                    [codeArray2 addObject:@"code"];
                    self.selectVC.dataArray = titleArray2;
                    self.selectVC.stateArray = [self creatStateArrayWithArray:titleArray2];
                    
                    self.buySelectVC.dataArray = titleArray2;
                    self.buySelectVC.stateArray = [self creatStateArrayWithArray:titleArray2];
                }
            }
        }
        if (responseDic[@"TradeKindPrice"]) {
            MSTradeKindPrice *priceModel = responseDic[@"TradeKindPrice"];
            saleContractPrice = priceModel.saleContactPrice;
            saleProjectPrice = priceModel.saleProjectPrice;
            buyContractPrice = priceModel.buyContactPrice;
            buyProjectPrice = priceModel.buyProjectPrice;
            _pricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币",saleContractPrice];
            _buyPricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币\n当前余额 %@",buyContractPrice,wallet];
        }
        if (responseDic[@"Wallet"]) {
            wallet = responseDic[@"Wallet"];
            _buyPricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币\n当前余额 %@",buyContractPrice,wallet];
        }
        if (responseDic[@"ReleaseSaleContacts"]) {
            [self releaseSuccess];
        }
        if (responseDic[@"ReleaseSaleProject"]) {
            [self releaseSuccess];
        }
        if (responseDic[@"ReleaseBuyContacts"]) {
            [self releaseSuccess];
        }
        if (responseDic[@"ReleaseBuyProject"]) {
            [self releaseSuccess];
        }
    }
}

- (void)releaseSuccess{
    if (self.choickBlock) {
        self.choickBlock();
    }
    [HDUtil showMessage:@"恭喜您，发布成功"];
    [rootNavigation popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self.view endEditing:YES];
        if (indexPath.row == 0){
            MSSelectProjectViewController *vc = [[MSSelectProjectViewController alloc] init];
            [rootNavigation pushViewController:vc animated:YES];
        }
        if (indexPath.row == 1){
            if (self.menu.selectedIndex == 0) {
                if ([self.selectVC.view superview]) {
                    [self.selectVC dismiss];
                } else {
                    [self.selectVC showInView:self.contentView];
                }
                
                if ([self.stateSelectVC.view superview]) {
                    [self.stateSelectVC dismiss];
                }
            }
            if (self.menu.selectedIndex == 1) {
                if ([self.buySelectVC.view superview]) {
                    [self.buySelectVC dismiss];
                } else {
                    [self.buySelectVC showInView:self.contentView];
                }
                
                if ([self.buyStateSelectVC.view superview]) {
                    [self.buyStateSelectVC dismiss];
                }
            }
        }
        if (indexPath.row == 2){
            if (self.menu.selectedIndex == 0) {
                if ([self.stateSelectVC.view superview]) {
                    [self.stateSelectVC dismiss];
                } else {
                    [self.stateSelectVC showInView:self.contentView];
                }
                
                if ([self.selectVC.view superview]) {
                    [self.selectVC dismiss];
                }
            }
            if (self.menu.selectedIndex == 1) {
                if ([self.buyStateSelectVC.view superview]) {
                    [self.buyStateSelectVC dismiss];
                } else {
                    [self.buyStateSelectVC showInView:self.contentView];
                }
                
                if ([self.buySelectVC.view superview]) {
                    [self.buySelectVC dismiss];
                }
            }
        }
        _selectedRow = indexPath.row;
    }
}

#pragma mark - HDMenuViewDelegate
- (void)selectMenuIndex:(int)index{
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*index, 0);
    [self.view endEditing:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.menu.selectedIndex = scrollView.contentOffset.x/SCREEN_WIDTH;
    [self.view endEditing:YES];
}

- (void)doneClick{
    [self.view endEditing:YES];
    NSArray *tipArray0 = @[@"请选择项目",@"请选择交易类别",@"请选择项目阶段",@"",];
    if (self.menu.selectedIndex == 0) {//出售
        NSMutableArray *sectionArray = self.tableVC.tradeArray[0];
        for (int i = 0; i<sectionArray.count; i++) {
            DOKeyValue *keyvalue = sectionArray[i];
            if ([keyvalue.value isEqualToString:@"未选择"]) {
                [HDUtil showMessage:tipArray0[i]];
                return;
            }
        }
        
        DOKeyValue *tradeType = sectionArray[1];
        if ([tradeType.value rangeOfString:@"联系人"].location != NSNotFound)
        {
            //联系人
            NSArray *tipArray = @[@"姓名不能空缺",@"公司",@"职位不能空缺",@"电话不能空缺",@"地址不能空缺",@"备注不能空缺",@"价格不能空缺"];
            NSMutableArray *sectionArray1 = self.tableVC.tradeArray[1];
            for (int i = 0; i<3; i++) {
                if (i==1) {
                    
                }
                else{
                    DOKeyValue *keyvalue = sectionArray1[i];
                    if ([[keyvalue.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                        [HDUtil showMessage:tipArray[i]];
                        return;
                    }
                }
            }
            if (_isAreaPhoneNum) {//有区号
                if ([[_areaPhonePrefix stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
                    //有空格，只输入了空格
                    [HDUtil showMessage:@"请输入区号"];
                    return;
                }
                if ([[_areaPhoneSuffix stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
                    //有空格，只输入了空格
                    [HDUtil showMessage:@"请输入电话号码"];
                    return;
                }
            }
            else{
                DOKeyValue *keyvalue = sectionArray1[3];
                if ([[keyvalue.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                    [HDUtil showMessage:tipArray[3]];
                    return;
                }
            }
            DOKeyValue *keyvalue6 = sectionArray1[6];
            if ([[keyvalue6.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                [HDUtil showMessage:tipArray[6]];
                return;
            }
            
            NSString *phoneStr = [NSString stringWithFormat:@"%@%@",_areaPhonePrefix,_areaPhoneSuffix];
            DOKeyValue *data = _sectionArray1[0];
            DOKeyValue *data1 = _sectionArray1[1];
            DOKeyValue *data2 = _sectionArray1[2];
            DOKeyValue *data3 = _sectionArray1[3];
            DOKeyValue *data4 = _sectionArray1[4];
            DOKeyValue *data5 = _sectionArray1[5];
            DOKeyValue *data6 = _sectionArray1[6];
            NSDictionary *dict = @{
                                   @"projectId":saleProjectIdStr,
                                   @"dealType":saleTypeStr,
                                   @"stage":saleStateStr,
                                   @"name":data.value,
                                   @"company":data1.value,
                                   @"position":data2.value,
                                   @"phone":phoneStr,
                                   @"mobile":data3.value,
                                   @"address":data4.value,
                                   @"remark":data5.value,
                                   @"price":data6.value,
                                   };
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
            [netEngine requestReleaseSaleContactsWithTarget:self.VctrID hudView:self.contentView params:dic];
        }
        else{
            NSMutableArray *sectionArray1 = self.tableVC.tradeArray[1];
            DOKeyValue *keyvalue = sectionArray1[0];
            if ([[keyvalue.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                [HDUtil showMessage:@"请填写价格"];
                return;
            }
            
            NSNumber *priceNum = [NSNumber numberWithInt:[[HDUtil check:keyvalue.value] intValue]];
            NSDictionary *dict = @{
                                   @"projectId":saleProjectIdStr,
                                   @"dealType":saleTypeStr,
                                   @"stage":saleStateStr,
                                   @"name":self.tableVC.textField.text,
                                   @"price":priceNum,
                                   @"remark":self.tableVC.remarkTextView.text};
            [netEngine requestReleaseSaleProjectWithTarget:self.VctrID hudView:self.contentView params:dict];
        }
        
    }
    if (self.menu.selectedIndex == 1) {//求购
        NSMutableArray *sectionArray = self.buyTableVC.tradeArray[0];
        for (int i = 0; i<sectionArray.count; i++) {
            DOKeyValue *keyvalue = sectionArray[i];
            if ([keyvalue.value isEqualToString:@"未选择"]) {
                [HDUtil showMessage:tipArray0[i]];
                return;
            }
        }
        
        DOKeyValue *tradeType = sectionArray[1];
        
        if ([tradeType.value rangeOfString:@"联系人"].location != NSNotFound)
        {//联系人
            NSArray *tipArray = @[@"姓名不能空缺",@"职位不能空缺",@"价格不能空缺"];
            NSMutableArray *sectionArray1 = self.buyTableVC.tradeArray[1];
            for (int i = 0; i<tipArray.count; i++) {
                DOKeyValue *keyvalue = sectionArray1[i];
                if ([[keyvalue.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                    [HDUtil showMessage:tipArray[i]];
                    return;
                }
            }
            
            DOKeyValue *data = _buySectionArray2[0];
            DOKeyValue *data1 = _buySectionArray2[1];
            DOKeyValue *data2 = _buySectionArray2[2];
            
            NSDictionary *dict = @{
                                   @"projectId":buyProjectIdStr,
                                   @"dealType":buyTypeStr,
                                   @"stage":@"",
                                   @"name":data.value,
                                   @"position":data1.value,
                                   @"price":data2.value};
            [netEngine requestReleaseBuyContactsWithTarget:self.VctrID hudView:self.contentView params:dict];
        }
        else{//进展
            NSMutableArray *sectionArray1 = self.buyTableVC.tradeArray[1];
            DOKeyValue *keyvalue = sectionArray1[0];
            if ([[keyvalue.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){//全是空格
                [HDUtil showMessage:@"请填写价格"];
                return;
            }
            
            NSDictionary *dict = @{
                                   @"projectId":buyProjectIdStr,
                                   @"dealType":buyTypeStr,
                                   @"stage":@"",
                                   @"name":self.buyTableVC.textField.text,
                                   @"remark":self.buyTableVC.remarkTextView.text,
                                   @"price":[HDUtil check:keyvalue.value]};
            [netEngine requestReleaseBuyProjectWithTarget:self.VctrID hudView:self.contentView params:dict];
        }
    }
}

- (void)areaPhoneNumEndEditIsArea:(BOOL)isArea prefix:(NSString*)prefixStr suffix:(NSString*)suffixStr{
    _isAreaPhoneNum = isArea;
    _areaPhonePrefix = prefixStr;
    _areaPhoneSuffix = suffixStr;
}

#pragma mark - MSFilterMultipleDelegate
- (void)selectedFilter{
    if (self.menu.selectedIndex == 0) {
        NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray *relutCodeArray = [[NSMutableArray alloc] initWithCapacity:0];

        NSMutableArray *tempDataArray = _selectedRow == 1? self.selectVC.dataArray:self.stateSelectVC.dataArray;
        NSMutableArray *tempStateArray = _selectedRow == 1? self.selectVC.stateArray:self.stateSelectVC.stateArray;
        NSArray *codeArray = _selectedRow == 1? netEngine.tradeKindArray:netEngine.projectStateArray;
        for (int i = 0; i< tempStateArray.count; i++) {
            NSString *stateStr = tempStateArray[i];
            if ([stateStr isEqualToString:@"YES"]) {
                [relutArray addObject:tempDataArray[i]];
                NSDictionary *dict = codeArray[i];
                [relutCodeArray addObject:[HDUtil check:dict[@"code"]]];
            }
        }
        if (_selectedRow == 1) {
            saleTypeStr = [relutCodeArray componentsJoinedByString:@","];
        }
        else{
            saleStateStr = [relutCodeArray componentsJoinedByString:@","];
        }
        
        NSString *resultStr = [relutArray componentsJoinedByString:@"、"];
        if (_selectedRow == 1) {
            if ([resultStr rangeOfString:@"联系人"].location != NSNotFound) {
                _tableVC.isProjectProcess = NO;
                [self.tableVC.tradeArray replaceObjectAtIndex:1 withObject:_sectionArray1];
                _pricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币",saleContractPrice];
            }
            else{
                _tableVC.isProjectProcess = YES;
                [self.tableVC.tradeArray replaceObjectAtIndex:1 withObject:@[[DOKeyValue key:@"价格" value:@"" placeHolder:@"未填写"]]];
                _pricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币",saleProjectPrice];
            }
        }
        
        resultStr = resultStr.length ? resultStr:@"未选择";
        
        NSMutableArray *sectionArray = self.tableVC.tradeArray[0];
        NSString *tempStr = _selectedRow == 1? @"交易类别":@"项目阶段";
        [sectionArray replaceObjectAtIndex:_selectedRow withObject:[DOKeyValue key:tempStr value:resultStr]];
        
        [_tableVC.tableView reloadData];
    }
    if (self.menu.selectedIndex == 1) {
        NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray *relutCodeArray = [[NSMutableArray alloc] initWithCapacity:0];

        NSMutableArray *tempDataArray = _selectedRow == 1? self.buySelectVC.dataArray:self.buyStateSelectVC.dataArray;
        NSMutableArray *tempDtateArray = _selectedRow == 1? self.buySelectVC.stateArray:self.buyStateSelectVC.stateArray;
        NSArray *codeArray = _selectedRow == 1? netEngine.tradeKindArray:netEngine.projectStateArray;

        for (int i = 0; i< tempDtateArray.count; i++) {
            NSString *stateStr = tempDtateArray[i];
            if ([stateStr isEqualToString:@"YES"]) {
                [relutArray addObject:tempDataArray[i]];
                NSDictionary *dict = codeArray[i];
                [relutCodeArray addObject:[HDUtil check:dict[@"code"]]];
            }
        }
        if (_selectedRow == 1) {
            buyTypeStr = [relutCodeArray componentsJoinedByString:@","];
        }
        else{
            buyTypeStr = [relutCodeArray componentsJoinedByString:@","];
        }
        
        NSString *resultStr = [relutArray componentsJoinedByString:@"、"];
        if (_selectedRow == 1) {
            if ([resultStr rangeOfString:@"联系人"].location != NSNotFound) {
                _buyTableVC.isProjectProcess = NO;
                [_buyTableVC.tradeArray replaceObjectAtIndex:1 withObject:_buySectionArray2];
                _buyPricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币\n当前余额 %@",buyContractPrice,wallet];
            }
            else{
                _buyTableVC.isProjectProcess = YES;
                [_buyTableVC.tradeArray replaceObjectAtIndex:1 withObject:@[[DOKeyValue key:@"价格" value:@"" placeHolder:@"未填写"]]];
                _buyPricelabel.text = [NSString stringWithFormat:@"该类消息平台平均交易价格 %@ 江湖币\n当前余额 %@",buyProjectPrice,wallet];
            }
        }
        resultStr = resultStr.length ? resultStr:@"未选择";
        
        NSMutableArray *sectionArray = self.buyTableVC.tradeArray[0];
        NSString *tempStr = _selectedRow == 1? @"交易类别":@"项目阶段";
        [sectionArray replaceObjectAtIndex:_selectedRow withObject:[DOKeyValue key:tempStr value:resultStr]];
        
        [_buyTableVC.tableView reloadData];
    }
}

- (void)tapClick{
    [self.view endEditing:YES];
}

#pragma mark - 选择完项目的通知
- (void)selectedProject:(NSNotification*)aNotification
{
    NSDictionary *dict = aNotification.userInfo;
    NSString *keyStr = [dict allKeys][0];
    NSString *valueStr = [dict allValues][0];

    if (self.menu.selectedIndex == 0) {
        NSMutableArray *sectionArray = self.tableVC.tradeArray[0];
        [sectionArray replaceObjectAtIndex:_selectedRow withObject:[DOKeyValue key:@"项目" value:keyStr]];
        [_tableVC.tableView reloadData];
        saleProjectIdStr = valueStr;
    }
    if (self.menu.selectedIndex == 1) {
        NSMutableArray *sectionArray = _buyTableVC.tradeArray[0];
        [sectionArray replaceObjectAtIndex:_selectedRow withObject:[DOKeyValue key:@"项目" value:keyStr]];
        [_buyTableVC.tableView reloadData];
        buyProjectIdStr = valueStr;
    }
}

#pragma mark - 键盘显示的时候的处理
- (void)keyboardWasShownLogin:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _tableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight-kbSize.height-40;
    _buyTableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight-kbSize.height-40;
    //    [self updateDataArray];
}

//键盘消失的时候的处理
- (void)keyboardWillBeHiddenLogin:(NSNotification*)aNotification
{
    _tableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight-90;
    _buyTableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight-90;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    else{
        return YES;
    }
}

@end
